﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using Kugar.Core.Data;
using Kugar.Core.Data.MSSQLHelper;
using  Kugar.Core.ExtMethod;
using Kugar.Core.Configuration;
using Kugar.Core.Security;

namespace Kugar.Core.Security.Providers.MSSQL
{
    public class MSSQL_PermissionProvider : PermissionProviderBase
    {

        private SQLHelperPool _pool = null;

        public MSSQL_PermissionProvider(string connectionName="RMSConnection")
        {
            var configItem = CustomConfigManager.Default.ConnectionSettings[connectionName];

            if (configItem==null)
            {
                throw new ConfigurationException("不存在名称为:" + connectionName + "的链接配置");
            }

            if (string.IsNullOrWhiteSpace(configItem.Value.ToStringEx()))
            {
                throw new ArgumentNullException("connectionName");
            }

            _pool=new SQLHelperPool(configItem.Value.ToStringEx(),5,1);

            base.PermissionManager = new MSSQL_PermissionCheckProvider(getConnection,true);
            base.GroupManager =new MSSQL_UserGroupManager(getConnection,true);
            base.UserSpecialPermissionManager =new MSSQL_UserSpecialManager(getConnection,true);
        }

        private SQLDataHelper getConnection()
        {
            return (SQLDataHelper) _pool.Take();
        }

        #region Overrides of PermissionProviderBase

        public override bool CheckUser(string userName, string password)
        {
            //TODO:验证用户
            return false;
        }

        #endregion
    }
}
